package HashText;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
public class Solution02 {

    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    public Node copyRandomList(Node head) {
      if(head == null){
          return null;
      }
      Map<Node,Node> map = new HashMap<>();
      Node cur =  head;
      while(cur != null){
          map.put(cur,new Node(cur.val));
          cur = cur.next;
      }
      cur =head;
      while(cur != null){
          Node copy = map.get(cur);
          if(cur.next != null){
              copy.next = map.get(cur.next);
          }
          if(cur.random != null){
              copy.random = map.get(cur.random);
          }
          cur = cur.next;
      }
      return map.get(head);
    }
}
